""" 
自定义异常处理器 
""" 
from rest_framework.views import exception_handler 
from rest_framework.exceptions import AuthenticationFailed 
from rest_framework.response import Response 
from rest_framework import status 

def custom_exception_handler(exc, context): 
    # 调用默认的异常处理器 
    response = exception_handler(exc, context) 
    
    # 如果是认证失败异常 
    if isinstance(exc, AuthenticationFailed): 
        return Response({ 
            'status': 401, 
            'data': { 
                'msg': '登录过期，请重新登录', 
                'code': 'TOKEN_EXPIRED' 
            } 
        }, status=status.HTTP_401_UNAUTHORIZED) 
    
    # 如果是其他401错误 
    if response is not None and response.status_code == status.HTTP_401_UNAUTHORIZED: 
        return Response({ 
            'status': 401, 
            'data': { 
                'msg': '登录过期，请重新登录', 
                'code': 'TOKEN_EXPIRED' 
            } 
        }, status=status.HTTP_401_UNAUTHORIZED) 
    
    return response  # 这里修复了语法错误